package com.fayou.cloud.cloudnacosconsumerorder8899.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.fayou.cloud.cloudnacosconsumerorder8899.myhandler.CustomerBlockHandler;
import com.fayou.cloud.cloudnacosconsumerorder8899.service.FeignPaymentService;
import com.fayou.cloud.entites.CommonResult;
import com.fayou.cloud.entites.Payment;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/consumer/payment")
public class OrderController {

    @Resource
    FeignPaymentService feignPaymentService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    public CommonResult selectOne(@RequestParam Integer id) {
        CommonResult payment = feignPaymentService.selectOne(id);
        return payment;
    }

    @GetMapping("/timeout")
    @SentinelResource(fallbackClass = {CustomerBlockHandler.class},fallback = "handlerException3")
    public String paymentFeignTimeout() {
        return feignPaymentService.paymentFeignTimeout();
    }


    @GetMapping("/rateLimit/byUrl")
    @SentinelResource(value = "byUrl", blockHandlerClass = {CustomerBlockHandler.class},blockHandler = "handlerException")
    public CommonResult byUrl() {
        return new CommonResult(200, "按url限流测试OK", new Payment(2020, 200d, null));
    }

    @GetMapping("/rateLimit1/byUrl")
    @SentinelResource(value = "byUrl", blockHandler = "dealbyUrl")
    public CommonResult byUrl1() {
        return new CommonResult(200, "按url限流测试OK", new Payment(2020, 200d, null));
    }


    public CommonResult dealbyUrl(BlockException exception) {

        return new CommonResult(200, "------deal_testHotKey,o(╥﹏╥)o", new Payment(2020, 200d, null));
    }


    @GetMapping("/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        return feignPaymentService.paymentInfo_OK(id);
    }

    @GetMapping("/hystrix/timeout/{id}")
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
        return feignPaymentService.paymentInfo_TimeOut(id);
    }

    //====服务熔断
    @GetMapping("/circuit/{id}")
    @SentinelResource(value = "paymentCircuitBreaker",
            blockHandlerClass = {CustomerBlockHandler.class},blockHandler = "handlerException4",fallbackClass = {CustomerBlockHandler.class},fallback = "handlerException3")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id) {
        return feignPaymentService.paymentCircuitBreaker(id);
    }
}
